using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;

namespace SCG.SolarVengeance.Scenarios
{
    public class UltimateBrainTester : Scenario
    {
        //ULTIMATE BRAIN TESTER V3
        //Close capital combates are now more frequent.
        //Added Pulsars and Blackholes (Blackholes are located far away from the cluster)

        //Create the Scenario
        public override void BuildScenario(int numPlayers, int capitalValues, int startingResources)
        {
            bool closecombat = (Random(0,100) <= 40);
            bool created = false;
            int cx = 0;
            int cy = 0;

            //Create Player 1's cluster
            int num = Random(11, 15);
            int maxnum = num;
            while (num > 0)
            {
                int x;
                int y;
                do
                {
                    if (closecombat == true && maxnum == num)
                    {
                        x = Random(46, 47);
                        y = Random(35, 65);
                    }
                    else
                    {
                        x = Random(25, 46);
                        y = Random(30, 70);
                    }
                }
                while (x > 49);
                int value = Random(1, 20);
                if (value == 1)
                    value = Random(1, 20);
                int shields = Random(0, value - 1);
                int scanners = (int)(SVGame.RNG.Next(value) * 2.0d * SVGame.RNG.NextDouble());
                if (scanners == 1)
                    scanners = 0;
                double resources = SVGame.RNG.Next(value) * 3.0d * SVGame.RNG.NextDouble();
                StarSystem system = CreateStarSystem(x, y, value, shields, scanners, resources);

                //Assign a bonus
                if (Random(1,100) > 90)
                {
                    system.BonusValue = (int)(SVGame.RNG.Next(100) * SVGame.RNG.NextDouble());
                    if (system.BonusValue < 5 || system.BonusValue > 90)
                        system.BonusValue = 0;
                    system.BonusShipType = StarShip.ShipTypes[SVGame.RNG.Next(StarShip.ShipTypes.Count)];
                }
                num--;
            }

            //Mirror Player1's cluster to create Player2's cluster
            num = Game.StarSystems.Count;
                for (int i = 0; i < num; i++)
                {
                    StarSystem system = Game.StarSystems[i];
                    StarSystem mirror = CreateStarSystem(100 - system.X, system.Y, system.Value, system.Shields, system.Scanners, system.Resources);
                    mirror.BonusShipType = system.BonusShipType;
                    mirror.BonusValue = system.BonusValue;
                }

            //Create some nebula
            int numNebula = SVGame.RNG.Next(2) + 1;
            while (numNebula > 0)
            {
                CreateNebula(SVGame.RNG.Next(40) + 10, SVGame.RNG.Next(50) + 25, SVGame.RNG.Next(4) + 2, SVGame.RNG.Next(40) + 35);
                numNebula--;
            }

            //and mirror them
            for (int x = 0; x < 50; x++)
                for (int y = 0; y < 100; y++)
                {
                    TerrainType t = GetTerrain(x, y);
                    SetTerrain(100 - x, y, t);
                }

            //create some wormholes
            int numWormholes = SVGame.RNG.Next(4) + 1;
            while (numWormholes > 0)
            {
                CreateWormhole(SVGame.RNG.Next(40) + 9, SVGame.RNG.Next(60) + 20, SVGame.RNG.Next(30) + 49, SVGame.RNG.Next(60) + 20);
                numWormholes--;
            }

            //and mirror them
            numWormholes = Game.Wormholes.Count;
            for (int w = 0; w < numWormholes; w++)
            {
                Wormhole wh = Game.Wormholes[w];
                int x = 100 - wh.X;
                int destx = 100 - wh.DestX;
                int y = wh.Y;
                int desty = wh.DestY;
                CreateWormhole(x, y, destx, desty);
            }

            //create some pulsars
            int numPulsars = SVGame.RNG.Next(3) + 1;
            while (numPulsars > 0)
            {
                created = false;
                while (created == false)
                {
                    cx = SVGame.RNG.Next(33) + 10;
                    cy = SVGame.RNG.Next(50) + 25;
                    if ((Distance(cx, cy, 49, 49) >= 12) & (Distance(cx, cy, 49, 49) <= 25)) created = true;
                }
                CreatePulsar(cx, cy);
                numPulsars--;
            }

            //and mirror them
            numPulsars = Game.Pulsars.Count;
            for (int w = 0; w < numPulsars; w++)
            {
                Pulsar pl = Game.Pulsars[w];
                int x = 100 - pl.X;
                int y = pl.Y;
                CreatePulsar(x,y);
            }

            //create some blackholes
            int numBlackholes = SVGame.RNG.Next(4) + 1;
            while (numBlackholes > 0)
            {
                created = false;
                while (created == false)
                {
                    cx = SVGame.RNG.Next(40) + 3;
                    cy = SVGame.RNG.Next(96) + 3;
                    if ((Distance(cx, cy, 49, 49) >= 35) & (Distance(cx, cy, 49, 49) <= 50))
                    {
                        created = true;
                        for (int w = 0; w < Game.BlackHoles.Count; w++)
                            if (Distance(cx, cy, Game.BlackHoles[w].X, Game.BlackHoles[w].Y) < 10)
                                created = false;
                    }

                }
                CreateBlackHole(cx, cy, SVGame.RNG.Next(5) + 7);
                numBlackholes--;
            }

            //and mirror them
            numBlackholes = Game.BlackHoles.Count;
            for (int w = 0; w < numBlackholes; w++)
            {
                BlackHole pl = Game.BlackHoles[w];
                int x = 100 - pl.X;
                int y = pl.Y;
                int r = pl.Radius;
                CreateBlackHole(x, y, r);
            }

            //Assign and mirror capitals
            numPlayers = Game.Players.Count;
            int idx = 0;
            while (numPlayers > 0)
            {
                //get a capital for player on left side
                StarSystem sys = null;
                if (closecombat == true)
                {
                    do
                    {
                        sys = Game.StarSystems[Random(0, Game.StarSystems.Count - 1)];
                    }
                    while ((sys.X < 46) | (sys.X > 54));
                }
                else
                {
                    do
                    {
                        sys = Game.StarSystems[Random(0, Game.StarSystems.Count - 1)];
                    }
                    while ((sys.X >= 43) & (sys.X <= 57));
                }
                Game.Players[idx].Capital = sys;

                //and mirror on right side
                idx++;
                numPlayers--;
                if (numPlayers > 0)
                {
                    SVMapObject svm = Game.GetObjectAt(100 - sys.X, sys.Y);
                    sys = svm as StarSystem;
                    Game.Players[idx].Capital = sys;
                    idx++;
                    numPlayers--;
                }
            }
        }

        //Scenario description
        public override string Description
        {
            get
            {
                return "This Scenario is similar to MirrorImage, but with Pulsars and Black Holes.  It also differs from MirrorImage in that:" + Environment.NewLine +                  
                    "1: There is no center to take over to control the enitre cluster. The middle of the map is no-mans land!" + Convert.ToChar(13) + Convert.ToChar(10) +
                    "2: The Capitals are never ridiculously close together. The Capitals will sometimes be within scanner range to preserve the close combat games." + Convert.ToChar(13) + Convert.ToChar(10) +
                    "3: This scenario encourages more skillful Brains to be developed so that there is more incentive for competition.";                    
            }
        }

        //Author
        public override string Author
        {
            get
            {
                return "Beat2k";
            }
        }
    }
}
